import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import './api/mock'
import Cookie from "js-cookie"

Vue.config.productionTip = false

// 全局引入Element-ui
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)

// 创建全局路由前置守卫
router.beforeEach((to,from,next)=>{
  // 判断token是否存在
  let token=Cookie.get('token')
  // token不存在  代表用户未登录
  if(!token && to.name!=="login"){
    next({name:'login'})
  }else if(token && to.name==="login"){
    next({name : 'home'})
  }else{
    next()
  }
})

new Vue({
  router,
  store,
  render: h => h(App),
  created(){
    store.commit('addMenu',router)
  }
}).$mount('#app')
